package com.ctgu.rabbitmq.rabbitmq.delayQueue.delayPlugin;

import com.ctgu.rabbitmq.entity.OrdersEntity;
import com.ctgu.rabbitmq.mapper.OrdersMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @ ClassName DelayPluginCustomer
 * @ Author Alex2
 * @ Date 2025/8/15 15:55
 **/
@Log4j2
@Component
public class DelayPluginCustomer {

    private static final String TAG = "Alex";

    private final OrdersMapper ordersMapper;

    public DelayPluginCustomer(OrdersMapper ordersMapper) {
        this.ordersMapper = ordersMapper;
    }

    @RabbitListener(queues = DelayPluginConfig.DLX_QUEUE_DELAY_PLUGIN)
    public void closeOrder(Integer orderId) {
        OrdersEntity order = ordersMapper.selectById(orderId);
        if (order != null && "UNPAID".equals(order.getStatus())) {
            order.setStatus("CLOSED");
            ordersMapper.updateById(order);
            log.info("{} : 订单 {} 超时关闭", TAG, orderId);
        }
    }
}